using System;
namespace Example77
{
///
/// Summary description for Class1.
///
class Class1
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main(string[] args)
{
int[] data = { 2, 5, 7, 12, 23, 34, 56, 78, 99, 123, 234, 345, 567 };
Console.Write("Enter the search key: ");
int key = int.Parse(Console.ReadLine());
int index = Search(data, key, 0, data.Length - 1);
if (index == -1)
Console.WriteLine("Key {0} was not found", key);
else
Console.WriteLine("Key {0} was found at index {1}",
key, index);
}
static int Search(int[] data, int key, int left, int right)
{
if (left <= right)
{
int middle = (left + right) / 2;
if (data[middle] == key)
return middle;
else
if (key < data[middle])
return Search(data, key, left, middle - 1);
else
return Search(data, key, middle + 1, right);
}
return -1;
}
}
}